import React from "react";
import Head from "next/head";

export default class extends React.Component {
  static async getInitialProps(ctx) {
    const i = ctx.query["id"];

    const zy_res = await fetch("https://www.tozsy.com/zy/homework", {
      method: "POST",
      credentials: "include",
      body: `{ "id" : ${i} }`,
    });
    const zy_json = await zy_res.json();
    let isExist = true;
    if (zy_json.state !== "success") {
      isExist = false;
    }
    console.log(zy_json);
    const homework = {
      isExist,
      expiration: zy_json.expiration,
      name: zy_json.name,
      single: zy_json.single,
      id: i,
    };

    let list = {};
    if (homework.isExist === true) {
      const list_res = await fetch("https://www.tozsy.com/zy/unhandle", {
        method: "POST",
        credentials: "include",
        body: `{ "id" : ${i} }`,
      });
      const list_json = await list_res.json();
      list = {
        isSuccess: list_json.state === "success",
        error: list_json.error,
        data: list_json.data,
      };
    }

    return {
      homework,
      list,
    };
  }

  constructor(props) {
    super(props);
    this.state = {
      homework: props.homework,
      list: props.list,
    };
  }

  async download() {
    const pre_res = await fetch("https://www.tozsy.com/zy/predownload", {
      method: "POST",
      credentials: "include",
      body: `{ "id" : ${this.state.homework.id} }`,
    });
    const pre_json = await pre_res.json();
    if (pre_json.state !== "success") {
      alert(pre_json.error);
    } else {
      pre_json.url = pre_json.url.replace("http://", "https://");
      window.open(pre_json.url);
    }
  }

  render() {
    console.log("---props---");
    console.log(this.props);
    console.log("---state---");
    console.log(this.state);
    return this.state.homework.isExist === true ? (
      <div>
        <Head>
          <title>{`【结果页】${this.state.homework.name}`}</title>
        </Head>
        <div>
          <div>{this.state.homework.name}</div>
          {(() => {
            const t = new Date(this.state.homework.expiration) - Date.now();
            if (t <= 0) {
              return <div>作业已截止</div>;
            } else {
              const c = new Date(t);
              console.log(c);
              return <div>作业仍未截止</div>;
            }
          })()}
        </div>
        <button onClick={() => this.download()}>打包下载</button>
        {this.state.list.isSuccess ? (
          <div>
            {this.state.list.data.length === 0 ? (
              <div>所有同学已交</div>
            ) : (
              <div>
                <div>未交同学如下：</div>
                {this.state.list.data.map((item) => {
                  return (
                    <div>
                      <div>{item.name}</div>
                      <div>{item.stuid}</div>
                    </div>
                  );
                })}
              </div>
            )}
          </div>
        ) : (
          <div>{this.state.list.error}</div>
        )}
      </div>
    ) : (
      <div>
        <Head>
          <title>作业不存在</title>
        </Head>
        <div>作业不存在</div>
      </div>
    );
  }
}
